package com.itheima.demo5.server;

import com.itheima.demo5.ui.User;

import java.io.*;
import java.net.Socket;

public class ServerThread extends Thread {

    private Socket socket;
    private DataInputStream dis;
    private DataOutputStream dos;


    public ServerThread(Socket socket) {
        this.socket = socket;
        try {
            dis = new DataInputStream(socket.getInputStream());
            dos = new DataOutputStream(socket.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                // 1. 登录， 2. 注册
                int type = dis.readInt();
                // 1. 读取客户端发来的用户名，密码
                String loginName = dis.readUTF();
                String loginPassword = dis.readUTF();
                switch (type) {
                    case 1:

                        if(check(loginName, loginPassword)){
                            // 登录成功 返回1， 登录失败 返回0
                            dos.writeInt(1);
                        }
                        else dos.writeInt(0);
                        break;
                    case 2:
                        if(!nameExist(loginName)){
                            // 注册成功 返回1， 注册失败 返回0
                            dos.writeInt(0);
                        }
                        else{
                            register(loginName, loginPassword);
                        }

                        break;
                }
            } catch (IOException e) {
                if(Server.map.get(socket) == null){
                    System.out.println("注册线程断开连接");
                    System.out.println("------------------------------------------");
                    break;
                }
                System.out.println("用户“" + Server.map.get(socket).getName() + "”断开连接");
                Server.map.remove(socket);
                System.out.println("------------------------------------------");
                break;
            }
        }
    }

    private void register(String loginName, String loginPassword) {
        try (
                DataOutputStream registerDos = new DataOutputStream(new FileOutputStream(Constant.documentPath, true));
                ){
            String line = loginName + "," + loginPassword + "\r\n";
            registerDos.writeUTF(line);
            try {
                // 注册成功 返回1
                dos.writeInt(1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private boolean check(String loginName, String loginPassword) throws IOException {
        try(
                DataInputStream registerDis = new DataInputStream(new FileInputStream(Constant.documentPath));
                ){
            while (true) {
                // 2. 读取文件中的所有用户名，密码，读到文件末尾，则退出循环
                if (registerDis.available() == 0) {
                    break;
                }
                String line = registerDis.readUTF();
                String[] arr = line.split(",");
                String name = arr[0];
                // password 去除末尾的 \r\n
                String password = arr[1].substring(0, arr[1].length() - 2);
                // 3. 比较
                if (loginName.equals(name) && loginPassword.equals(password)) {
                    Server.map.put(socket, new User(loginName, loginPassword));
                    return true;
                }
            }
        } catch (IOException e){
            e.printStackTrace();
        }
        return false;
    }

    private boolean nameExist(String loginName){
        try (
                DataInputStream registerDis = new DataInputStream(new FileInputStream(Constant.documentPath));
                ){
            while (true) {
                if (registerDis.available() == 0) {
                    break;
                }
                String line = registerDis.readUTF();
                if(line.equals("")) break;
                String[] arr = line.split(",");
                String name = arr[0];
                if(name.equals(loginName)) return false;
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return true;
    }
}
